<template>
  <div class="tracetask-container">
    <v-layout wrap justify-center align-center>
      <v-flex md12 d-flex>
        <v-flex md2 px-2>
          <v-text-field v-model="queryForm.shopName" label="店铺名称"></v-text-field>
        </v-flex>
        <!-- <v-flex md2 px-2>
          <v-select :items="countries" v-model="queryForm.country" label="国家"></v-select>
        </v-flex> -->
        <!-- <v-flex md2 px-2>
          <v-select :items="platforms" v-model="queryForm.platform" label="平台"></v-select>
        </v-flex> -->
        <v-flex md2 px-2>
          <v-select :items="buyerStatus" v-model="queryForm.checkStatus" label="状态"></v-select>
        </v-flex>
        <v-flex md1>
          <v-btn round class="subTitle" color="primary" @click="onQuery">
            <span>查询</span>
            <v-icon>search</v-icon>
          </v-btn>
        </v-flex>
        <!-- <v-flex md1>
          <v-btn round dark class="subTitle" color="indigo" @click="onAdd">
            <span>添加店铺</span>
            <v-icon>add</v-icon>
          </v-btn>
        </v-flex> -->
      </v-flex>

      <!-- table -->
      <v-flex md12 class="elevation-1">
        <v-data-table :headers="headers" :loading="loading" :items="tableData" hide-actions>
          <template v-slot:items="props">
            <td class="text-xs-center">{{ props.item.shopId }}</td>
            <td class="text-xs-center">{{ props.item.checkStatus | TopupStatus }}</td>
            <td class="text-xs-center">{{ props.item.checkNickName }}</td>
            <td class="text-xs-center">{{ props.item.platform | Platform }}</td>
            <td class="text-xs-center">{{ props.item.shopName }}</td>
            <!-- <td class="text-xs-center">{{ props.item.category }}</td> -->
            <td class="text-xs-center">{{ props.item.countTaskNum }}</td>
            <td class="text-xs-center">{{ props.item.countOrderNum }}</td>
            <td class="text-xs-center">{{ getTime(props.item.createTime) }}</td>
            <td class="text-xs-center">{{ getTime(props.item.modifiedTime) }}</td>
            <td width="25%" class="text-xs-center ">
              <v-layout wrap justify-center align-center>
                <v-flex md4>
                  <v-btn
                    round
                    small
                    outline
                    class="subTitle"
                    color="indigo lighten-2"
                    @click="onAudit(props.item)"
                  >
                    <div class="mr-1">店铺审核</div>
                  </v-btn>
                </v-flex>
                <v-flex md4>
                  <v-btn
                    round
                    small
                    outline
                    class="subTitle"
                    color="teal lighten-1"
                    @click="onUserInfo(props.item)"
                  >
                    <div class="mr-1">用户信息</div>
                  </v-btn>
                </v-flex>
              </v-layout>
            </td>
          </template>
          <template v-slot:no-data>
            <v-alert :value="true" outline color="warning" icon="info">暂无数据</v-alert>
          </template>
        </v-data-table>

        <!-- <v-layout class="pagination-wrapper">
          <v-flex md10 d-flex justify-start align-center>
            <v-flex d-flex md2 justify-start align-center>
              <v-flex md4 class="title-color">显示</v-flex>
              <v-flex md4>
                <v-select :items="pageOptions" v-model="pagination.limit"></v-select>
              </v-flex>
              <v-flex md4 class="title-color">条/页</v-flex>
            </v-flex>
            <v-flex md2 class="title-color">共 {{ pagination.pageCount }} 条数据</v-flex>
            <v-flex md2></v-flex>
            <v-flex md4>
              <v-pagination
                @input="onPageChange"
                :length="pagination.pageCount"
                circle
                total-visible="5"
                v-model="pagination.page"
              />
            </v-flex>
            <v-flex md2></v-flex>
          </v-flex>
        </v-layout> -->
      </v-flex>
    </v-layout>

    <v-dialog v-model="dialog" max-width="800px">
      <v-card>
        <v-card-title class="headline grey lighten-2" primary-title>{{ dialogTitle }}</v-card-title>
        <v-card-text>
          <!-- 上传评价图片 -->
          <v-layout justify-center align-center v-if="dialogType === 1">
            <v-flex></v-flex>
          </v-layout>
        </v-card-text>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn color="blue darken-1" flat @click="onDialogConfirm">确认</v-btn>
          <v-btn color="grey darken-1" flat @click="dialog = false">取消</v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>

    <v-dialog v-model="modal" persistent max-width="290">
      <v-card>
        <v-card-title class="title">{{ modalTitle }}</v-card-title>
        <v-card-text class="title">{{ modalContent }}</v-card-text>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn color="blue darken-1" flat @click="onModalConfirm">确定</v-btn>
          <v-btn color="grey darken-1" flat @click="modal = false">取消</v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
  </div>
</template>


<script lang="ts">
// 使用类装饰器
import { Component, Vue, Mixins } from "vue-property-decorator";
import { Todo } from "@/api";
import { TopupStatusKV, PageOptions } from "@/utils/constants";
import { KV } from "@/utils/type";
import { forEach } from "lodash";
import { ReqProfitIncome } from "@/api/bean";
import { PlatformKV, CountryKV } from "@/utils/constants";
import { getTime } from "@/utils/commonUtils";
import Cookie from 'js-cookie';
declare const $Vue: Vue;

@Component
export default class OrderList extends Vue {
  private pageOptions: any = [];
  private buyerStatus: any = [
  {
    text: "全部",
    value: null
  },
  {
    text: "待处理",
    value: 0
  },
  {
    text: "审核通过",
    value: 1
  },
  {
    text: "审核未通过",
    value: -1
  }
];

  private platforms: any = PlatformKV;
  private countries: any = CountryKV;

  private queryForm: any = {
    // taskId: "",
    // site: "",
    // status: ''
  };

  private headers: Array<any> = [
    { text: "ID", align: "center", sortable: false, value: "id" },
    { text: "状态", align: "center", sortable: false, value: "seller" },
    { text: "审核人", align: "center", sortable: false, value: "seller" },
    { text: "平台", align: "center", sortable: false, value: "taskId" },
    { text: "店铺名称", align: "center", sortable: false, value: "id" },
    // { text: "店铺类目", align: "center", sortable: false, value: "id" },
    { text: "活动数量", align: "center", sortable: false, value: "platform" },
    { text: "订单数量", align: "center", sortable: false, value: "type" },
    { text: "创建时间", align: "center", sortable: false, value: "myshop" },
    { text: "更新时间", align: "center", sortable: false, value: "myshop" },
    { text: "操作", align: "center", sortable: false, value: "action" }
  ];

  private tableData: any = [
    {
      username: "合适的"
    }
  ];
  // 表格选中项目
  private selected: Array<any> = [];

  private loading: boolean = false;

  private dialog: boolean = false;
  private dialogTitle: string = "";
  private dialogType: number = 1;
  private dialogItem: any = {};


  private modal: boolean = false;
  private modalType: number = 1;
  private modalItem: any = {};
  private modalTitle: string = "注意!";
  private modalContent: string = "确认取消订单吗?";
  private isQuery: boolean = false;

  private pagination: any = {
    pageCount: 20,
    page: 1,
    limit: 10,
    offset: 0
  };

  protected mounted() {
    this.getShopList();
    this.initialize();
  }
  private getTime(date: string) {
    return getTime(date);
  }

  private initialize() {
    // this.buyerStatus = TopupStatusKV.slice();
    this.pageOptions = PageOptions.slice();
  }
  private changeSort() {}

  private toggleAll() {
    if (this.selected.length) this.selected = [];
    else this.selected = this.tableData.slice();
  }

  private onAdd() {
    this.$router.push({
      name: '添加店铺',
    });
  }

  private onQuery(from?: string) {
    let params: any = {};
    let valid = 0;
    forEach(this.queryForm, (item, key) => {
      // item ? (
        params[key] = item
        // ) : valid++;
    });
    if (valid === 2) {
      // return $Vue.$emit("snackbar", {
      //   message: "请至少填写一个查询条件"
      // });
    }
    if (from === undefined) {
      this.pagination.offset = 0;
      this.pagination.page = 1;
    }
    params.limit = this.pagination.limit;
    params.offset = this.pagination.offset;
    this.getShopList(params);
    this.isQuery = true;
  }

  // 审核
  private onAudit(item: any) {
    this.$router.push({
      name: "店铺审核",
      params: item,
    });
  }

  private onUserInfo(item: any) {
    // this.modal = true;
    // this.modalContent = "确定删除该店铺吗?";
    // this.modalType = 1;
    // this.modalItem = item;
    this.dialog = true;
    this.dialogTitle = '用户信息';
    this.dialogType = 1;
    this.dialogItem = item;
  }

  private onBatchAudit() {}

  private onPageChange(value: any) {
    let limit = this.pagination.limit;
    this.pagination.offset = (value - 1) * limit;
    if (!this.isQuery) {
      this.getShopList();
    } else {
      this.onQuery("wyvonj");
    }
  }

  private onModalConfirm() {
    if (this.modalType === 1) {
      // this.deleteShop();
    }
  }

  private onDialogConfirm() {
    // 
  }

  /**
   * 获取店铺列表
   */
  private async getShopList(queryForm?: any) {
    let params: ReqProfitIncome = {
      // offset: this.pagination.offset,
      // limit: this.pagination.limit,
      // userId: Cookie.get('userId'),
    };
    if (queryForm) {
      params = queryForm;
    }
    const { data } = await Todo.getShopList.r(params);
    console.log("getShopList", data);
    if (data.result === 0) {
      let $data = data.data;
      this.tableData = $data.dataList;
        // this.pagination.pageCount = $data.totalCount
        // ? Math.ceil($data.totalCount / this.pagination.limit)
        // : 1;
    } else {
      $Vue.$emit("snackbar", {
        message: data.message
      });
    }
  }

  /**
   * 删除店铺
   */
  private async deleteShop() {
    // checkType 2 提现
    let params: ReqProfitIncome = {
      // offset: this.pagination.offset,
      // limit: this.pagination.limit
    };
    const { data } = await Todo.getShopList.r(params);
    console.log("getShopList", data);
    if (data.result === 0) {
      this.getShopList();
    } else {
      $Vue.$emit("snackbar", {
        message: data.message
      });
    }
  }
}
</script>

<style lang="less" scoped>
.tracetask-container {
  width: 100%;
  height: 100%;
}
.pagination-wrapper {
  background: white;
  border-top: 1px solid #d4d4d4;
}
</style>


